package com.huitone.smspfm.context.app;

import java.io.IOException;
import java.io.InputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.bin.common.utils.ArrayUtils;
import com.bin.common.utils.StringUtils;


/**   
* @Title AppOutStatusListener.java 
* @Package com.huitone.smspfm.context.app 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年4月18日 下午5:52:52 
* @version V1.0   
*/
public class AppOutStatusListener extends Thread {
	
	private static final Logger logger = LogManager.getLogger(AppOutStatusListener.class);
			//Logger.getLogger(AppOutStatusListener.class);
	
	private ServerSocket serverSocket = null;
	
	private AppStatusListener statusListener = null;
	
	private int listenPort = 9999;
	
	private String cfgfile = null;

	private static AppOutStatusListener instance = new AppOutStatusListener();
	
	public static AppOutStatusListener getInstance() {
		return instance;
	}
	
	private AppOutStatusListener() {}

	public void run() {
		
		try {
			serverSocket = new ServerSocket(listenPort);
			
			statusListener.start(this);
			
			while(AppManager.isRunning()) {
				
				Socket accept = serverSocket.accept();
				
				accept.setSoTimeout(10000);
				
				InputStream inputStream = accept.getInputStream();
				
				byte[] buffer = new byte[280];
				
				inputStream.read(buffer);
				
				cfgfile = StringUtils.strip(StringUtils.toString(ArrayUtils.subarray(buffer, 24, 280), null));
				
				dispatchCommand(StringUtils.strip(StringUtils.toString(ArrayUtils.subarray(buffer, 0, 24), null)));
				
				inputStream.close();
				
				accept.close();
			}
			
			serverSocket.close();
			
		} catch (BindException e) {
			logger.error(listenPort + "端口被占用,启动失败.");
		} catch (SocketException e) {
			logger.info("应用状态监听器关闭,应用结束.");
		} catch (IOException e) {
			logger.error("应用状态监听器IO异常,应用关闭.");
		}
		
		AppManager.shutdown();
	}
	
	public void dispatchCommand(String command) throws IOException {
		
		logger.info(listenPort + "端口接收到命令:" + command);
		
		switch (command) {
		case Command.SHUTDOWN:
			
			AppManager.shutdown();
			
			if (serverSocket != null) {
				serverSocket.close();
			}
			
			if (statusListener != null) {
				statusListener.stop(this);
			}
			
			break;
			
		case Command.RELOAD:
			
			if (statusListener != null) {
				statusListener.reload(this);
			}
			
			break;
		}
		
	}
	
	public interface AppStatusListener {
		void start(AppOutStatusListener appOutStatusListener);
		void stop(AppOutStatusListener appOutStatusListener);
		void reload(AppOutStatusListener appOutStatusListener);
	}
	
	
	public interface Command {
		String SHUTDOWN = "shutdown";
		String RESTART = "restart";
		String RELOAD = "reload";
	}


	public AppOutStatusListener setAppStatusListener(AppStatusListener statusListener) {
		this.statusListener = statusListener;
		return this;
	}

	public AppOutStatusListener setListenPort(int listenPort) {
		this.listenPort = listenPort;
		return this;
	}

	public static void main(String[] args) {
		
		
	}

	public String getCfgfile() {
		return cfgfile;
	}

	public void setCfgfile(String cfgfile) {
		this.cfgfile = cfgfile;
	}
}
